package com.fjh.springboot_01.validation;

import com.fjh.springboot_01.anno.State;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class StateValidation implements ConstraintValidator<State, String> {
    /**
     *
     * @param value 将来要校验的数据
     * @param context context in which the constraint is evaluated
     *
     * @return 如果返回false,则校验不通过,如果返回true,则校验通过
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 如果value为null，直接返回true，因为有@NotEmpty注解会处理空值校验
        if (value == null) {
            return true;
        }
        // 校验状态值是否合法
        return "已发布".equals(value) || "草稿".equals(value);
    }
}
